function buildAdmin(result, tableId, bodyId){
    var input = document.querySelector('#on');
    var oList = document.querySelector(tableId);
    var oTBody =oList.querySelector(bodyId);
    var question = input.innerHTML;
    var str ='';
    if(question == '用户管理'){
        for(var user of result.data.userInfoList){
            var uid = user.id;
            str += `
            <tr>
                <td>${user.id}</td>
                <td>${user.username}</td>
                <td>${user.password}</td>
                <td>${user.admin}</td>
                <td><button class="delete" onclick="del(this)" id="${uid}">删除</button></td>
                <td><button class="update iconfont" onclick="update(this)" id="${uid}">&#xe66e;</button></td>
                </tr>
            `
            }
            oTBody.innerHTML = str;
        }
    else if(question == '题目管理'){
        for(var problem of result.data.problemInfoList){
        var pid = problem.id;
        str += `
        <tr>
            <td>${problem.id}</td>
            <td>${problem.title}</td>
            <td>${problem.level}</td>
            <td>${problem.description}</td>
            <td>${problem.templateCode}</td>
            <td>${problem.testCode}</td>
            <td>${problem.visitCount}</td>
            <td>${problem.acCount}</td>
            <td><button class="delete" onclick="del(this)" id="${pid}">删除</button></td>
            <td><button class="update iconfont" onclick="update(this)" id="${pid}">&#xe66e;</button></td>
            </tr>
        `
        }
        oTBody.innerHTML = str;
    }
}

function del(obj) {
    var input = document.querySelector('#on');
    var question = input.innerHTML;
    var id = obj.id;
    var url;
    if(question == '用户管理'){
        url = 'deleteUser/' + id;
    }else if(question == '题目管理'){
        url = 'deleteProblem/' + id;
    }
    jQuery.ajax({
        url: url,
        method: 'get',
        headers: {
            'Authorization': localStorage.getItem("token")
        },
        success: function(result){
            if(result != null && result.code == 200){
                alert('操作成功！');
                adminSearch();
            }else{
                alert('操作失败！');
            }
        }
    })
}


var popUp = document.querySelector('.mask');
var problemPop = document.querySelector('.problem-pop');
var userPop = document.querySelector('.user-pop')
var ensure = document.querySelectorAll('.pop .submit');
function update(obj){
    popUp.style.display='block';
    var input = document.querySelector('#on');
    var question = input.innerHTML;
    var tr = obj.parentNode.parentNode;
    if(question == '用户管理'){
        problemPop.style.display='none';
        userPop.style.display = 'block';
        var child = tr.firstElementChild;
        var idInput = document.querySelector('#uid');
        idInput.value = child.innerHTML;

        child = child.nextElementSibling;
        var usernameInput = document.querySelector('#username');
        usernameInput.value = child.innerHTML;

        child = child.nextElementSibling;
        var passwordInput = document.querySelector('#password');
        passwordInput.value = child.innerHTML;

        child = child.nextElementSibling;
        var adminInput = document.querySelector('#admin');
        adminInput.value = child.innerHTML;

        if(child.innerHTML == 0){
            passwordInput.setAttribute('disabled', 'disabled');
        }

    }else if(question == '题目管理'){
        // 获取要修改的信息的父元素
        userPop.style.display = 'none';
        problemPop.style.display='block';
        var child = tr.firstElementChild;
        var idInput = document.querySelector('#pid');
        idInput.value = child.innerHTML;

        child = child.nextElementSibling;
        var titleInput = document.querySelector('#title');
        titleInput.value = child.innerHTML;

        child = child.nextElementSibling;
        var levelInput = document.querySelector('#level');
        levelInput.value = child.innerHTML;

        child = child.nextElementSibling;
        var descInput = document.querySelector('#desc');
        descInput.value = child.innerHTML;

        child = child.nextElementSibling;
        var modelCoding = document.querySelector('#modelCoding');
        modelCoding.value = child.innerHTML;

        child = child.nextElementSibling;
        var testCoding = document.querySelector('#testCoding');
        testCoding.value = child.innerHTML;
    }
}

function Close(){
    popUp.style.display = 'none';
    ensure.onclick = null;
    var input = document.querySelector('#on');
    var question = input.innerHTML;
    if(question == '用户管理'){
        var idInput = document.querySelector('#uid');
        idInput.value = '';
        var usernameInput = document.querySelector('#username');
        usernameInput.value = '';
        var passwordInput = document.querySelector('#password');
        passwordInput.value = '';
        passwordInput.removeAttribute('disabled', 'disabled');
        var adminInput = document.querySelector('#admin');
        adminInput.value = '';
    }else if(question == '题目管理'){
        var idInput = document.querySelector('#pid');
        idInput.value = '';
        var titleInput = document.querySelector('#title');
        titleInput.value = '';
        var levelInput = document.querySelector('#level');
        levelInput.value = '';
        var descInput = document.querySelector('#desc');
        descInput.value = '';
        var modelCoding = document.querySelector('#modelCoding');
        modelCoding.value = '';
        var testCoding = document.querySelector('#testCoding');
        testCoding.value = '';
    }
}


function submit(){
    var updateData;
    var input = document.querySelector('#on');
    var question = input.innerHTML;
    var url;
    if(question == '用户管理'){
        var id = document.querySelector('#uid');
        var username = document.querySelector('#username');
        var password = document.querySelector('#password');
        var admin = document.querySelector('#admin');
        updateData = {
            id: id.value,
            password: password.value,
            username: username.value,
            admin: admin.value
        };
        url = 'updateUser';
    }else if(question == '题目管理'){
        var id = document.querySelector('#pid');
        var title = document.querySelector('#title');
        var level = document.querySelector('#level');
        var desc = document.querySelector('#desc');
        var modelCoding = document.querySelector('#modelCoding');
        var testCoding = document.querySelector('#testCoding');
        updateData = {
            id: id.value,
            title: title.value,
            level: level.value,
            desc: desc.value,
            modelCoding: modelCoding.value,
            testCoding: testCoding.value

        };
        url = 'updateProblem';
    }
    jQuery.ajax({
        url: url,
        method: 'post',
        contentType:'application/json',
        data: JSON.stringify(updateData),
        dataType:'json',
        headers: {
          'Authorization': localStorage.getItem("token")
        },
        success: function(result){
            if(result != null && result.code == 200){
                alert('操作成功！');
                Close();
                adminSearch();
            }else{
                alert('操作失败！');
            }
        }
    }) 
}


function add(){
    popUp.style.display='block';
    var input = document.querySelector('#on');
    var question = input.innerHTML;
    if(question == '用户管理'){
        problemPop.style.display='none';
        userPop.style.display = 'block';
    }else if(question == '题目管理'){
        userPop.style.display = 'none';
        problemPop.style.display='block';
    }
}